'''
Description: 
Author: Zilu
Date: 2021-11-08 19:16:36
Version: 
LastEditTime: 2021-11-08 20:49:57
LastEditors: Zilu
'''

# open 函数打开一个文件对象
# f = open('/Users/michael/text.txt', 'r')

# 打开错误抛出IOError错误，成功则返回文件对象
import os
os.getcwd()
f = open('./test.txt', 'r')
print(f.read())
f.close()

# 可以用try ... finally 保证文件一定关闭，也可以用 with 语句
with open('./test.txt', 'r') as f:
    print(f.read())

# readline() 读取一行，readlines()读取多行
f = open('./test.txt', 'r')
for line in f.readlines():
    print(line.strip())
f.close()

# 二进制读取
f = open('./test.txt', 'rb')
f.read()

f = open('./test.txt', 'r', encoding='utf-8')
f.read()

# 有些编码不规范的文件，可能会产生UnicodeDecodeError，还可以使open接受以及errors参数，表示如何处理这些错误
f = open('./test.txt', 'r', encoding='utf-8', errors='ignore')

# open参数'w'写入，'a'追加写入。可以参考官方文档：https://docs.python.org/3/library/functions.html#open




